import React, {
  useState,
  useEffect,
  useLayoutEffect,
  useCallback,
} from "react";
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from "@ant-design/icons";
import { Button, Layout, Menu, theme, Select } from "antd";
import { Outlet, NavLink, Link, useNavigate, Navigate } from "react-router-dom";
import { menuRoutes } from "./router";

const { Header, Sider, Content } = Layout;

const App: React.FC = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  const navigate = useNavigate();


  const [menuItems, setMenuItems] = useState([
    {
      key: "1",
      icon: <UserOutlined />,
      label: "nav 1",
    },
  ]);

  const initMenuItem = useCallback((routes: any[]) => {
    const items = routes.map((m) => {
      const res: any = {};
      res.key = m.path;
      if (m.icon) res.icon = m.icon;
      res.label = m.label;
      if (m.children && m.children.length > 0) {
        res.children = initMenuItem(m.children);
      }
      return res;
    });
    return items;
  }, []);

  useLayoutEffect(() => {
    setMenuItems(initMenuItem(menuRoutes));
  }, []);

  const clickMenu = useCallback((obj) => {
    navigate(obj.key);
  }, []);

  return (
    <Layout style={{ height: "100vh" }}>
      <Navigate to="/data"></Navigate>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={["/data"]}
          items={menuItems}
          onClick={clickMenu}
        />
      </Sider>
      <Layout>
        <Header
          style={{
            padding: 0,
            background: colorBgContainer,
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between",
          }}
        >
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: "16px",
              width: 64,
              height: 64,
            }}
          />
        </Header>
        <Content
          style={{
            margin: "24px 16px",
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          <Outlet />
        </Content>
      </Layout>
    </Layout>
  );
};

export default App;
